import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { erpNumberFormatter, erpPriceInputFormatter } from '@vben/utils';

import { z } from '#/adapter/form';
import { getAccountSimpleList } from '#/api/erp/finance/account';
import { getProductSimpleList } from '#/api/erp/product/product';
import { getSupplierSimpleList } from '#/api/erp/purchase/supplier';
import { getWarehouseSimpleList } from '#/api/erp/stock/warehouse';
import { getSimpleUserList } from '#/api/system/user';
import { getRangePickerDefaultProps } from '#/utils';

import { $t } from '#/locales';

/** 表单的配置项 */
export function useFormSchema(formType: string): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'no',
      label: $t('views.erp.purchase.return.no'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.generateBySystem'),
        disabled: true,
      },
    },
    {
      fieldName: 'returnTime',
      label: $t('views.erp.purchase.return.returnTime'),
      component: 'DatePicker',
      componentProps: {
        disabled: formType === 'detail',
        placeholder: $t('views.erp.purchase.return.returnTimePlaceholder'),
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
      rules: 'required',
    },
    {
      fieldName: 'orderNo',
      label: $t('views.erp.purchase.return.orderNo'),
      component: 'Input',
      formItemClass: 'col-span-1',
      rules: 'required',
      componentProps: {
        placeholder: $t('views.erp.purchase.return.orderNoPlaceholder'),
        disabled: formType === 'detail',
      },
    },
    {
      fieldName: 'supplierId',
      label: $t('views.erp.common.supplier'),
      component: 'ApiSelect',
      componentProps: {
        disabled: true,
        placeholder: $t('views.erp.common.supplierPlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getSupplierSimpleList,
        fieldNames: {
          label: 'name',
          value: 'id',
        },
      },
      rules: 'required',
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
        autoSize: { minRows: 1, maxRows: 1 },
        disabled: formType === 'detail',
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'fileUrl',
      label: $t('views.erp.common.fileUrl'),
      component: 'FileUpload',
      componentProps: {
        maxNumber: 1,
        maxSize: 10,
        accept: [
          'pdf',
          'doc',
          'docx',
          'xls',
          'xlsx',
          'txt',
          'jpg',
          'jpeg',
          'png',
        ],
        showDescription: formType !== 'detail',
        disabled: formType === 'detail',
      },
      formItemClass: 'col-span-3',
    },
    {
      fieldName: 'items',
      label: $t('views.erp.common.productItem.title'),
      component: 'Input',
      formItemClass: 'col-span-3',
    },
    {
      fieldName: 'discountPercent',
      label: $t('views.erp.common.discountPercent'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.common.discountPercentPlaceholder'),
        min: 0,
        max: 100,
        precision: 2,
      },
      rules: z.number().min(0).optional(),
    },
    {
      fieldName: 'discountPrice',
      label: $t('views.erp.common.discountPrice'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.common.discountPricePlaceholder'),
        precision: 2,
        formatter: erpPriceInputFormatter,
        disabled: true,
      },
    },
    {
      fieldName: 'discountedPrice',
      label: $t('views.erp.common.discountedPrice'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.common.discountedPricePlaceholder'),
        precision: 2,
        formatter: erpPriceInputFormatter,
        disabled: true,
      },
      dependencies: {
        triggerFields: ['totalPrice', 'otherPrice'],
        componentProps: (values) => {
          const totalPrice = values.totalPrice || 0;
          const otherPrice = values.otherPrice || 0;
          values.discountedPrice = totalPrice - otherPrice;
          return {};
        },
      },
    },
    {
      fieldName: 'otherPrice',
      label: $t('views.erp.common.otherPrice'),
      component: 'InputNumber',
      componentProps: {
        disabled: formType === 'detail',
        placeholder: $t('views.erp.common.otherPricePlaceholder'),
        precision: 2,
        formatter: erpPriceInputFormatter,
      },
    },
    {
      fieldName: 'accountId',
      label: $t('views.erp.common.account'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.common.accountPlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getAccountSimpleList,
        fieldNames: {
          label: 'name',
          value: 'id',
        },
      },
    },
    {
      fieldName: 'totalPrice',
      label: $t('views.erp.purchase.return.totalPrice'),
      component: 'InputNumber',
      componentProps: {
        precision: 2,
        min: 0,
        disabled: true,
      },
      rules: z.number().min(0).optional(),
    },
  ];
}

/** 表单的明细表格列 */
export function useFormItemColumns(
  formData?: any[],
): VxeTableGridOptions['columns'] {
  return [
    { type: 'seq', title: $t('views.common.seq'), minWidth: 50, fixed: 'left' },
    {
      field: 'warehouseId',
      title: $t('views.erp.common.warehouse'),
      minWidth: 200,
      slots: { default: 'warehouseId' },
    },
    {
      field: 'productId',
      title: $t('views.erp.common.product'),
      minWidth: 200,
      slots: { default: 'productId' },
    },
    {
      field: 'stockCount',
      title: $t('views.erp.common.productItem.stockCount'),
      minWidth: 100,
      formatter: 'formatAmount3',
    },
    {
      field: 'productBarCode',
      title: $t('views.erp.common.productItem.productBarCode'),
      minWidth: 120,
    },
    {
      field: 'productUnitName',
      title: $t('views.erp.common.productItem.unit'),
      minWidth: 80,
    },
    {
      field: 'remark',
      title: $t('common.remark'),
      minWidth: 150,
      slots: { default: 'remark' },
    },
    {
      field: 'inCount',
      title: $t('views.erp.purchase.return.inCount'),
      formatter: 'formatAmount3',
      minWidth: 120,
      fixed: 'right',
      visible: formData && formData[0]?.inCount !== undefined,
    },
    {
      field: 'returnCount',
      title: $t('views.erp.purchase.return.returnCount'),
      formatter: 'formatAmount3',
      minWidth: 120,
      fixed: 'right',
      visible: formData && formData[0]?.returnCount !== undefined,
    },
    {
      field: 'count',
      title: $t('views.erp.common.productItem.count'),
      minWidth: 120,
      fixed: 'right',
      slots: { default: 'count' },
    },
    {
      field: 'productPrice',
      title: $t('views.erp.common.productItem.productPrice'),
      fixed: 'right',
      minWidth: 120,
      slots: { default: 'productPrice' },
    },
    {
      field: 'totalProductPrice',
      fixed: 'right',
      title: $t('views.erp.common.productItem.totalProductPrice'),
      minWidth: 130,
      formatter: 'formatAmount2',
    },
    {
      fixed: 'right',
      field: 'taxPercent',
      title: $t('views.erp.common.taxPercent'),
      minWidth: 110,
      slots: { default: 'taxPercent' },
    },
    {
      fixed: 'right',
      field: 'taxPrice',
      title: $t('views.erp.common.taxPrice'),
      minWidth: 120,
      formatter: 'formatAmount2',
    },
    {
      field: 'totalPrice',
      fixed: 'right',
      title: $t('views.erp.common.productItem.totalPrice'),
      minWidth: 120,
      formatter: 'formatAmount2',
    },
    {
      title: $t('common.action'),
      width: 80,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'no',
      label: $t('views.erp.purchase.return.no'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.purchase.return.noPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'productId',
      label: $t('views.erp.common.product'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.common.productPlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getProductSimpleList,
        fieldNames: {
          label: 'name',
          value: 'id',
        },
      },
    },
    {
      fieldName: 'returnTime',
      label: $t('views.erp.purchase.return.returnTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'supplierId',
      label: $t('views.erp.common.supplier'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.common.supplierPlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getSupplierSimpleList,
        fieldNames: {
          label: 'name',
          value: 'id',
        },
      },
    },
    {
      fieldName: 'warehouseId',
      label: $t('views.erp.common.warehouse'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.common.warehousePlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getWarehouseSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'creator',
      label: $t('views.common.creatorName'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.common.creatorNamePlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getSimpleUserList,
        fieldNames: {
          label: 'nickname',
          value: 'id',
        },
      },
    },
    {
      fieldName: 'orderNo',
      label: $t('views.erp.purchase.return.orderNo'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.purchase.return.orderNoPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'refundStatus',
      label: $t('views.erp.purchase.return.refundStatus'),
      component: 'Select',
      componentProps: {
        options: [
          { label: '未退款', value: 0 },
          { label: '部分退款', value: 1 },
          { label: '全部退款', value: 2 },
        ],
        placeholder: $t('views.erp.purchase.return.refundStatusPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.ERP_AUDIT_STATUS, 'number'),
        placeholder: $t('common.statusPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Input',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'checkbox',
      width: 50,
      fixed: 'left',
    },
    {
      field: 'no',
      title: $t('views.erp.purchase.return.no'),
      width: 200,
      fixed: 'left',
    },
    {
      field: 'productNames',
      title: $t('views.erp.purchase.order.productNames'),
      showOverflow: 'tooltip',
      minWidth: 120,
    },
    {
      field: 'supplierName',
      title: $t('views.erp.common.supplier'),
      minWidth: 120,
    },
    {
      field: 'returnTime',
      title: $t('views.erp.purchase.return.returnTime'),
      width: 160,
      formatter: 'formatDateTime',
    },
    {
      field: 'creatorName',
      title: $t('views.common.creatorName'),
      minWidth: 120,
    },
    {
      field: 'totalCount',
      title: $t('views.erp.purchase.return.totalCount'),
      formatter: 'formatAmount3',
      minWidth: 120,
    },
    {
      field: 'totalPrice',
      title: $t('views.erp.purchase.return.totalPrice'),
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'refundPrice',
      title: $t('views.erp.purchase.return.refundPrice'),
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'unRefundPrice',
      title: $t('views.erp.purchase.return.noRefundPrice'),
      formatter: ({ row }) => {
        return `${erpNumberFormatter(row.totalPrice - row.refundPrice, 2)}元`;
      },
      minWidth: 120,
    },
    {
      field: 'status',
      title: $t('common.status'),
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ERP_AUDIT_STATUS },
      },
    },
    {
      title: $t('common.action'),
      minWidth: 250,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
/** 列表的搜索表单 */
export function useOrderGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'no',
      label: $t('views.erp.purchase.order.no'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.purchase.order.noPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'productId',
      label: $t('views.erp.common.product'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.common.productPlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getProductSimpleList,
        fieldNames: {
          label: 'name',
          value: 'id',
        },
      },
    },
    {
      fieldName: 'orderTime',
      label: $t('views.erp.purchase.order.orderTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useOrderGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'radio',
      width: 50,
      fixed: 'left',
    },
    {
      field: 'no',
      title: $t('views.erp.purchase.order.no'),
      width: 200,
      fixed: 'left',
    },
    {
      field: 'productNames',
      title: $t('views.erp.purchase.order.productNames'),
      showOverflow: 'tooltip',
      minWidth: 120,
    },
    {
      field: 'supplierName',
      title: $t('views.erp.common.supplier'),
      minWidth: 120,
    },
    {
      field: 'orderTime',
      title: $t('views.erp.purchase.order.orderTime'),
      width: 160,
      formatter: 'formatDate',
    },
    {
      field: 'creatorName',
      title: $t('views.common.creatorName'),
      minWidth: 120,
    },
    {
      field: 'totalCount',
      title: $t('views.erp.purchase.order.totalCount'),
      formatter: 'formatAmount3',
      minWidth: 120,
    },
    {
      field: 'inCount',
      title: $t('views.erp.purchase.order.inCount'),
      formatter: 'formatAmount3',
      minWidth: 120,
    },
    {
      field: 'returnCount',
      title: $t('views.erp.purchase.order.returnCount'),
      formatter: 'formatAmount3',
      minWidth: 120,
    },
    {
      field: 'totalProductPrice',
      title: $t('views.erp.purchase.order.totalProductPrice'),
      formatter: 'formatAmount2',
      minWidth: 130,
    },
    {
      field: 'totalPrice',
      title: $t('views.erp.purchase.order.totalPrice'),
      formatter: 'formatAmount2',
      minWidth: 120,
    },
  ];
}
